package com.fenghuajueli.libbasecoreui.upload

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.upload.UploadUtils.payAction
import com.fenghuajueli.libbasecoreui.upload.UploadUtils.registerAction
import com.fenghuajueli.libbasecoreui.upload.honor.HonorUploadUtils
import com.fenghuajueli.libbasecoreui.upload.huawei.HuaweiUploadUtils
import com.fenghuajueli.libbasecoreui.upload.oppo.OppoUploadUtils
import com.fenghuajueli.libbasecoreui.upload.vivo.VivoUploadUtils
import com.fenghuajueli.libbasecoreui.upload.xiaomi.XiaomiUploadUtils

@Deprecated(message = "已弃用,后台控制的归因", replaceWith = ReplaceWith("OppoUploadUtils,VivoUploadUtils,XiaomiUploadUtils"))
object UploadUtils {

    private val channels = Channel.entries
    private val needUploadOperation = HashMap<Channel, Boolean>(channels.size)

    init {
        channels.forEach { channel ->
            needUploadOperation[channel] = channel.isUploadNeeded()
        }
    }

    @JvmStatic
    fun openAction(lifecycleOwner: LifecycleOwner, application: Application) {
        channels.forEach { channel ->
            if (needUploadOperation[channel] == false) {
                return
            }
            channel.openAction(lifecycleOwner, application)
        }
    }

    @JvmStatic
    fun registerAction(
        lifecycleOwner: LifecycleOwner,
        application: Application,
        needCheck: Boolean
    ) {
        channels.forEach { channel ->
            if (needUploadOperation[channel] == false) {
                return
            }
            channel.registerAction(lifecycleOwner, application, needCheck)
        }
    }

    @JvmStatic
    fun payAction(lifecycleOwner: LifecycleOwner, application: Application, payAmount: Double) {
        channels.forEach { channel ->
            if (needUploadOperation[channel] == false) {
                return
            }
            channel.payAction(lifecycleOwner, application, payAmount)
        }
    }

    private fun Channel.openAction(lifecycleOwner: LifecycleOwner, application: Application) {
        when (this) {
            Channel.OPPO -> OppoUploadUtils.instance.openAction(lifecycleOwner, application)
            Channel.VIVO -> VivoUploadUtils.instance.openAction(lifecycleOwner, application)
            Channel.XIAOMI -> XiaomiUploadUtils.instance.openAction(lifecycleOwner, application)
            Channel.Huawei -> HuaweiUploadUtils.instance.openAction(application)
            Channel.HONOR -> HonorUploadUtils.instance.openAction(application)
        }
    }

    private fun Channel.registerAction(
        lifecycleOwner: LifecycleOwner,
        application: Application,
        needCheck: Boolean
    ) {
        when (this) {
            Channel.OPPO -> OppoUploadUtils.instance.registerAction(application)
            Channel.VIVO -> VivoUploadUtils.instance.registerAction(application)
            Channel.XIAOMI -> XiaomiUploadUtils.instance.registerAction(application)
            Channel.Huawei -> HuaweiUploadUtils.instance.registerAction(application)
            Channel.HONOR -> HonorUploadUtils.instance.registerAction(application)
        }
    }

    private fun Channel.payAction(
        lifecycleOwner: LifecycleOwner,
        application: Application,
        payAmount: Double
    ) {
        when (this) {
            Channel.OPPO -> OppoUploadUtils.instance.payAction(application, payAmount)
            Channel.VIVO -> VivoUploadUtils.instance.payAction(application, payAmount)
            Channel.XIAOMI -> XiaomiUploadUtils.instance.payAction(application, payAmount)
            Channel.Huawei -> HuaweiUploadUtils.instance.payAction(application, payAmount)
            Channel.HONOR -> HonorUploadUtils.instance.payAction(application, payAmount)
        }
    }

    private fun Channel.isUploadNeeded(): Boolean {
        return SwitchKeyUtils.getChannelUploadSwitch(name)
    }
}